﻿using System;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;

using Telerik.WebControls;
using UFSoft.UBF.UI.WebControls;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.Util.Log;
using UFSoft.UBF.Util.Globalization;

using UFSoft.UBF.UI.IView;
using UFSoft.UBF.UI.Engine;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using System.Collections.Generic;
using UFSoft.UBF.UI;
using System.Web;



/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/
namespace ItemRef
{
    public partial class ItemRefWebPart
    {
        #region Custome eventBind
        public override bool UseQueryCase
        {

            get { return true; }

        }

        //FindButton_Click...
        private void FindButton_Click_Extend(object sender, EventArgs  e)
		{
			//调用模版提供的默认实现.--默认实现可能会调用相应的Action.
			
		
			FindButton_Click_DefaultImpl(sender,e);
		}	
		 
				//QryButton_Click...
		private void QryButton_Click_Extend(object sender, EventArgs  e)
		{
			//调用模版提供的默认实现.--默认实现可能会调用相应的Action.
			
		
			QryButton_Click_DefaultImpl(sender,e);
		}	
		 

			

		//DDLCase_TextChanged...
		private void DDLCase_TextChanged_Extend(object sender, EventArgs  e)
		{
			//调用模版提供的默认实现.--默认实现可能会调用相应的Action.
			
		
			DDLCase_TextChanged_DefaultImpl(sender,e);
		}	
		 
				//ConfirmButton_Click...
		private void ConfirmButton_Click_Extend(object sender, EventArgs  e)
		{
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.

            ReturnSelectedValue();

            ConfirmButton_Click_DefaultImpl(sender,e);
		}	
		 
				//CancelButton_Click...
		private void CancelButton_Click_Extend(object sender, EventArgs  e)
		{
			//调用模版提供的默认实现.--默认实现可能会调用相应的Action.
			
		
			CancelButton_Click_DefaultImpl(sender,e);
		}	
		 
			
				

		//DataGrid_GridRowDbClicked...
		private void DataGrid_GridRowDbClicked_Extend(object sender, GridDBClickEventArgs  e)
		{
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.

            ReturnSelectedValue();

            DataGrid_GridRowDbClicked_DefaultImpl(sender,e);
		}


        private void ReturnSelectedValue()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(long));
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("PurchaseUOM_ID", typeof(long));
            dt.Columns.Add("PurchaseUOM_Code", typeof(string));
            dt.Columns.Add("PurchaseUOM_Name", typeof(string));
            foreach (IUIRecord _frd in this.Model.cRef.SelectRecords)
            {

                DataRow dr = dt.NewRow();
                dr["ID"] = _frd["ID"];
                dr["Code"] = _frd["Code"];
                dr["Name"] = _frd["Name"];
                dr["PurchaseUOM_ID"] = _frd["PurchaseUOM_ID"];
                dr["PurchaseUOM_Code"] = _frd["PurchaseUOM_Code"];
                dr["PurchaseUOM_Name"] = _frd["PurchaseUOM_Name"];
                dt.Rows.Add(dr);
            }

            this.CurrentState["CustItem_Table"] = dt;
        }


        /// <summary>
        /// 返回选定参照值到一个控件上。
        /// </summary>
        private void ReturnSelectedValueMule()
        {
            string returnXml = "";
            StringBuilder sbID = new StringBuilder();
            StringBuilder sbCode = new StringBuilder();
            StringBuilder sbName = new StringBuilder();
            UIRuntimeHelper helper = UIRuntimeHelper.Instance;

            IList<IUIRecord> Records = helper.GetSelectRecordFromCache(this.Model.cRef);

            int recordCount = Records.Count;
            for (int i = 0; i < recordCount; i++)
            {
                if (sbID.Length != 0)
                {
                    sbID.Append(",");
                    sbCode.Append(",");
                    sbName.Append(",");
                }

                sbID.Append(((cRefRecord)Records[i])["ID"].ToString());
                sbCode.Append(((cRefRecord)Records[i])["Code"].ToString());
                sbName.Append(((cRefRecord)Records[i])["Name"]);
            }

            returnXml += "<ReferenceReturn>";
            returnXml += "<ReturnField name='ID' value='" + new Random().Next(1000) + "'/>";
            returnXml += "<ReturnField name='Code' value='" + System.Web.HttpUtility.HtmlEncode(sbCode.ToString()) + "'/>";
            returnXml += "<ReturnField name='Name' value='" + System.Web.HttpUtility.HtmlEncode(sbName.ToString()) + "'/>";
            returnXml += "<ReturnField name='Description' value='" + sbID.ToString() + "'/>";
            returnXml += "</ReferenceReturn>";

            string xml = "<ReferenceReturns>";
            xml += returnXml;
            xml += "</ReferenceReturns>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            xml = doc.DocumentElement.OuterXml;
            //xml = xml.Replace("\"", "'");
            xml = HttpContext.Current.Server.HtmlEncode(xml);
            string script = "<script language=\"javascript\">";
            script += "window.returnValue = \"" + xml + "\";";
            script += "window.close();";
            script += "</script>";
            AtlasHelper.RegisterStartupScript((Control)TopLevelContainer, this.GetType(), "ReferenceReturn", script, false);
        }



        #region 自定义数据初始化加载和数据收集
        private void OnLoadData_Extend(object sender)
		{	
			OnLoadData_DefaultImpl(sender);
		}
		private void OnDataCollect_Extend(object sender)
		{	
			OnDataCollect_DefaultImpl(sender);
		}
		#endregion  

        #region 自己扩展 Extended Event handler 
		public void AfterOnLoad()
		{

		}

        public void AfterCreateChildControls()
        {


		
        }
        
        public void AfterEventBind()
        {
        }
        
		public void BeforeUIModelBinding()
		{

		}

		public void AfterUIModelBinding()
		{


		}


        #endregion
		
        #endregion
		
    }
}